package com.china08.yxyapi.api.manage.stu.mystudy.mistakes;

import java.text.ParseException;
import java.util.Date;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.MediaType;
import org.springframework.web.bind.annotation.CrossOrigin;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;

import com.china08.yxyapi.api.YxyApiBaseController;
import com.china08.yxyapi.model.YxyApiReqModel;
import com.china08.yxyapi.model.YxyApiRespModel;
import com.china08.yxyapi.repository.mg.mistakes.StuMistakesQuesRepository;
import com.china08.yxyapi.util.DateToolUtils;

/**
 * 错题本--学生端--搜索结果
 * 
 * @author LiuWeidong
 *
 */
@CrossOrigin
@RestController
public class YxyMistakesQuesCountController extends YxyApiBaseController {

    @Autowired
    private StuMistakesQuesRepository stuMistakesQuesRepository;

    @RequestMapping(value = "/api_yw/manage/stu/mystudy/mistake/count", method = RequestMethod.POST, produces = MediaType.APPLICATION_JSON_UTF8_VALUE)
    public RespModel precess(HttpServletRequest request, HttpServletResponse response, @RequestBody ReqModel reqModel,
            RespModel respModel) throws ParseException {
        String studentId = getProfile(request, response).getId();
        String subject = reqModel.subject;
        Integer status = reqModel.status;
        Integer focus = reqModel.focus;
        Date startDate = reqModel.startDate;
        Date endDate = reqModel.endDate;

        Integer count = stuMistakesQuesRepository.countByStudentIdAndSubjectAndStatusAndFocusAndPublishDateBetween(
                studentId, subject, status, focus, startDate, endDate);
        RespModel model = new RespModel();
        model.count = count;
        String aa = DateToolUtils.format(reqModel.startDate, DateToolUtils.FORMAT_SHORT_YMD);
        model.startDate = aa;
        String bb = DateToolUtils.format(reqModel.endDate, DateToolUtils.FORMAT_SHORT_YMD);
        model.endDate = bb;
        return model;
    }

    /* 请求参数 */
    protected static class ReqModel extends YxyApiReqModel {
        // 学科
        public String subject;
        // 状态： 0--未消灭 1--已消灭
        public Integer status;
        // 划分：0--普通 1--重点题
        public Integer focus;
        // 试题发布时间
        // public Date publishDate;
        // 筛选开始时间
        public Date startDate;
        // 筛选结束时间
        public Date endDate;
    }

    /* 响应参数 */
    protected static class RespModel extends YxyApiRespModel {
        // 错题数量
        public Integer count;
        // 学科
        public String subject;
        // 开始时间
        public String startDate;
        // 结束时间
        public String endDate;

    }
}
